/*
 * Parse a library file
 *
 */

package edu.ujf.sharepas.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IFile;

import edu.ujf.sharepas.impl.Constante.CategoryType;

/**
 *
 * @author yaiba
 */
public class Parser {

	/**
	 * format du fichier 
	 * 	category:pathsFile
	 */
	
	public static final int DESCRIPTION = 4;
	
	/**
	 * analyse file and return the library associated to the user
	 * @param userFile
	 * @return
	 */
	public static Library getLibrary(User user){
		String filePath = user.getLibFilePath();
		Library lib = getLibrary(user,filePath);
		lib.setOwner(user);
		return lib;
	}
	
	public static Library getLibrary(User user , String userF){
		try{
			File userFile = new File(userF);
			
			if( userFile.exists() &&  userFile.canRead() ){
				BufferedReader f = new BufferedReader((new FileReader(userFile)));
				String line = null;
				Library lib = new Library(null);
				int typeCat = 0;
				int typeRight = 0;
				String name = "";
				Category c = null;
				List<String> header = new ArrayList<String>();
				for(int i=0; i < 4; i++){
					header.add(f.readLine());
				}
				user.setId(Integer.parseInt(header.get(0)));
				user.setMessage((header.get(1)));
				user.setAvatar((header.get(2)));
				

				line = f.readLine();
				while( line!=null ){
					String[] list = line.split(";");
					typeCat = Integer.parseInt(list[0]);
					typeRight = Integer.parseInt(list[2]);
					if(typeRight != -1){
						c = lib.addCategory(typeCat);
						c.addShareFile(list[1],typeRight);
						line = f.readLine();
					}else{
						c = lib.addCategory(typeCat);
						line = f.readLine();
					}
				}
				lib.setCompteur(Integer.parseInt(header.get(3)));
				return lib;
			}else{
				File f = new File(Constante.getSharePasProperty(Constante.Property.USERLIB.toString()));
				if( !f.exists() ){
					f.createNewFile();
					
				}
				BufferedWriter fw = new BufferedWriter(new FileWriter(f));
				fw.write(user.getId()+"\n");
				fw.write(user.getName()+"\n");
				fw.write(user.getAvatar()+"\n");
				fw.write(1+"\n");
				fw.close();
				return new Library();
			}
			
		}catch(Exception e){
			return new Library();	
		}
		
	}
	
	public static void Save(Library lib){
		try{
			File ftmp = null;
			if( lib.getOwner() instanceof Friend ){
				ftmp = new File(Constante.getSharePasProperty(Constante.Property.FRIEND_PATH.toString())+lib.getOwner().getId()+".lib");
			}else{
				ftmp = new File(Constante.getSharePasProperty(Constante.Property.USERLIB.toString()));
			}
			int compt = 1;
			if( ftmp.exists() ){
				BufferedReader fr = null;
				if( lib.getOwner() instanceof Friend ){
					System.out.println("test");
					fr = new BufferedReader(new FileReader(Constante.getSharePasProperty(Constante.Property.FRIEND_PATH.toString())+lib.getOwner().getName()+".lib"));
					System.out.println("test");
				}else{
					fr = new BufferedReader(new FileReader(Constante.getSharePasProperty(Constante.Property.USERLIB.toString())));
				}
				System.out.println("test");
				if( fr == null ){
					fr.readLine();
					fr.readLine();
					fr.readLine();
					// compteur 
					compt = Integer.parseInt(fr.readLine());
				}
				System.out.println("test");
	
				fr.close();
			}
			System.out.println("test");

			System.out.println("comt "+compt+" lib "+lib.getCompteur());
			if( compt > lib.getCompteur() ){
				return ;
			}
			BufferedWriter f = null;
			//File ftmp = new File()
			if( lib.getOwner() instanceof Friend ){
				f = new BufferedWriter(new FileWriter(Constante.getSharePasProperty(Constante.Property.FRIEND_PATH.toString())+lib.getOwner().getId()+".lib"));
			}else{
				f = new BufferedWriter(new FileWriter(Constante.getSharePasProperty(Constante.Property.USERLIB.toString())));
			}
			System.out.println("test");
			User u = lib.getOwner();
			
			f.write(Integer.toString(u.getId())+"\n");
			f.write(u.getMessage()+"\n");
			f.write(u.getAvatar()+"\n");
			f.write(u.getUserLib().getCompteur()+"\n");
			for(Category c:lib.getListCategory()){
				if(c.getListShareFile().size() == 0){
					f.write(c.getType()+";null;-1\n");
				}else{
					for(ShareFile s:c.getListShareFile()){
						f.write(c.getType()+";"+s.getIFile().getLocation()+";"+s.getRight().toInt()+"\n");
					}
				}
			}
			f.close();
		}catch(Exception e){
			
		}
	}
	
	public static void Save(Library lib, String fileName){
		try{
			
			BufferedWriter f = new BufferedWriter(new FileWriter(fileName));
			
			for(Category c:lib.getListCategory()){
				if(c.getListShareFile().size() == 0){
					f.write(c.getType()+";null;-1\n");
				}else{
					for(ShareFile s:c.getListShareFile()){
						f.write(c.getType()+";"+s.getIFile().getLocation()+";"+s.getRight().toInt()+"\n");
					}
				}
				
			}
			f.close();
		}catch(Exception e){
			
		}
	}
}
